﻿using System;
using System.Text;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;

namespace Neatly.Module
{
    public class NewWebRequestCertificate : CertificateHandler
    {
        protected override bool ValidateCertificate(byte[] certificateData)
        {
            return true;
        }
    }

    public class HttpModule : Singleton<HttpModule>
    {
        public async UniTask<(string, string)> Get(string url, string session = null)
        {
            UnityWebRequest request = UnityWebRequest.Get(url);
            request.certificateHandler = new NewWebRequestCertificate();
            request.timeout = 5;
            if (!string.IsNullOrEmpty(session))
            {
                request.SetRequestHeader("Authorization", "Bearer " + session);
            }
            try
            {
                await request.SendWebRequest();
            }
            catch (Exception e)
            {
                return (e.Message, "");
            }
            if (request.result == UnityWebRequest.Result.Success) return request.responseCode == 200 ? ("", request.downloadHandler.text) : (request.responseCode.ToString(), "");
            Debug.LogError("http 请求错误:" + request.error);
            return (request.error, "");
        }

        public async UniTask<(string, string)> PostJson(string url, string data, string session = null)
        {
            byte[] bodyRaw = Encoding.UTF8.GetBytes(data);
            UnityWebRequest request = new UnityWebRequest(url, "POST");
            request.certificateHandler = new NewWebRequestCertificate();
            request.timeout = 5;
            if (!string.IsNullOrEmpty(data))
            {
                request.uploadHandler = new UploadHandlerRaw(bodyRaw);
                request.downloadHandler = new DownloadHandlerBuffer();
            }

            request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
            if (!string.IsNullOrEmpty(session))
            {
                request.SetRequestHeader("Authorization", "Bearer " + session);
            }
            try
            {
                await request.SendWebRequest();
            }
            catch (Exception e)
            {
                return (e.Message, "");
            }
            if (request.result == UnityWebRequest.Result.Success) return request.responseCode == 200 ? ("", request.downloadHandler.text) : (request.responseCode.ToString(), "");
            Debug.LogError($"http 请求错误:{request.responseCode} {request.error}");
            return (request.error, "");
        }

        public async UniTask<(string, string)> PostForm(string url, WWWForm form)
        {
            var request = UnityWebRequest.Post(url, form);
            request.certificateHandler = new NewWebRequestCertificate();
            request.timeout = 5;
            try
            {
                await request.SendWebRequest();
            }
            catch (Exception e)
            {
                return (e.Message, "");
            }
            if (request.result == UnityWebRequest.Result.Success) return request.responseCode == 200 ? ("", request.downloadHandler.text) : (request.responseCode.ToString(), "");
            Debug.LogError("http 请求错误:" + request.error);
            return (request.error, "");
        }

        public async UniTask<(string, byte[])> GetByte(string url, string session = null)
        {
            UnityWebRequest request = UnityWebRequest.Get(url);
            request.certificateHandler = new NewWebRequestCertificate();
            request.timeout = 5;
            if (!string.IsNullOrEmpty(session))
            {
                request.SetRequestHeader("Authorization", "Bearer " + session);
            }
            try
            {
                await request.SendWebRequest();
            }
            catch (Exception e)
            {
                return (e.Message, null);
            }
            if (request.result == UnityWebRequest.Result.Success) return request.responseCode == 200 ? ("", request.downloadHandler.data) : (request.responseCode.ToString(), null);
            Debug.LogError("http 请求错误:" + request.error);
            return (request.error, null);
        }
    }
}